//
//  LMTitleSegment.m
//  benben
//
//  Created by mac on 16/5/16.
//  Copyright © 2016年 xunao. All rights reserved.
//

#import "LMTitleSegement.h"

@interface LMTitleSegement()

@property(nonatomic, strong) NSMutableArray <UIButton *> *segmentArray;

/** 底部滑动的白条 */
@property(nonatomic, strong) UIView  *bottomLine;
/** 被选中的分页按钮 */
@property(nonatomic, weak)UIButton *selectedSegment;

@end

@implementation LMTitleSegement

#pragma - mark 懒加载
-(NSMutableArray *)segmentArray{
    if (_segmentArray == nil) {
        _segmentArray = [NSMutableArray array];
    }
    return _segmentArray;
}

-(UIView *)bottomLine{
    if (_bottomLine == nil) {
        _bottomLine = [[UIView alloc] init];
        _bottomLine.backgroundColor = kColor4B79AF;
    }
    return _bottomLine;
}
#pragma - mark 系统方法
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.bottomLine];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat btnY = 0;
    CGFloat leftRightBoder = 10 * __kFixScaleToIP5;
    CGFloat segmentW = (self.width / self.segmentArray.count) - leftRightBoder;
    CGFloat segmentH = self.height - 3;
    int i = 0;
    for (UIButton * segmentBtn in self.segmentArray) {
        segmentBtn.left = leftRightBoder + i * segmentW;
        segmentBtn.top = btnY;
        segmentBtn.width = segmentW;
        segmentBtn.height = segmentH;
        i++;
    }
    //底部白条
    self.bottomLine.width = 115;//底部滑动条的宽度
    self.bottomLine.height = 2;
    self.bottomLine.bottom = self.height - 1;
    CGPoint lineCenter = self.bottomLine.center;
    lineCenter.x = self.selectedSegment.center.x;
    self.bottomLine.center = lineCenter;
}

#pragma - public 
@synthesize selectedIndex = _selectedIndex;
/** 设置选中的标签 */
-(void)setSelectedIndex:(NSInteger)selectedIndex {
    _selectedIndex = selectedIndex;
    [self segementSelected:self.segmentArray[selectedIndex]];
}

-(NSInteger)selectedIndex {
    return self.selectedSegment.tag;
}

#pragma - mark 事件方法
/** 添加分页按钮 */
-(UIButton *)addSegmentBtnWithTitle:(NSString *)title{
    
    UIButton *btn = [[UIButton alloc] init];
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    btn.tag = self.segmentArray.count;
    
    btn.titleLabel.font = _kFontMain;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:kColorA6A6A6 forState:UIControlStateNormal];
    [btn setTitleColor:kColor4B79AF forState:UIControlStateDisabled];
    [btn addTarget:self action:@selector(segementSelected:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    self.selectedSegment = self.selectedSegment ? self.selectedSegment : btn;
    self.selectedSegment.enabled = NO;
    [self.segmentArray addObject:btn];
    [self setNeedsLayout];
    return btn;
}

/** 按钮点击 */
-(void)segementSelected:(UIButton *)segement {
    segement.enabled = NO;
    self.selectedSegment.enabled = YES;
    self.selectedSegment = segement;
    self.userInteractionEnabled = NO;
    [UIView animateWithDuration:0.36 animations:^{
        CGPoint lineCenter = self.bottomLine.center;
        lineCenter.x = self.selectedSegment.center.x;
        self.bottomLine.center = lineCenter;
    } completion:^(BOOL finished) {
        
        self.userInteractionEnabled = YES;
    }];
    if ([self.delegate respondsToSelector:@selector(titleSegementDidClickAtIndex:)]) {
        [self.delegate titleSegementDidClickAtIndex:segement.tag];
    }
    
}

@end
